import { isEqual as isEqualRow } from 'lodash-es';

export const isEqual = isEqualRow;

const toString = Object.prototype.toString

export function is(val: unknown, type: string) {
    return toString.call(val) === `[object ${type}]`
}

export function isDef<T = unknown>(val?: T): val is T {
    return typeof val !== 'undefined'
}

export function isUnDef<T = unknown>(val?: T): val is T {
    return !isDef(val)
}

export function isEmpty<T = unknown>(val: T): val is T {
    if (isArray(val) || isString(val)) {
        return val.length === 0
    }

    if (val instanceof Map || val instanceof Set) {
        return val.size === 0
    }

    if (isObject(val)) {
        return Object.keys(val).length === 0
    }

    return false
}

export function isSetObject<T = any>(params: any): params is Set<T> {
    return is(params, 'Set')
}

export function isObject<T = any>(params: any): params is Record<string, T> {
    return is(params, 'Object')
}

export function isDate(val: unknown): val is Date {
    return is(val, 'Date')
}

export function isNull(val: unknown): val is null {
    return val === null
}

export function isNullAndUnDef(val: unknown): val is null | undefined {
    return isUnDef(val) && isNull(val)
}

export function isNullOrUnDef(val: unknown): val is null | undefined {
    return isUnDef(val) || isNull(val)
}

export function isNullOrEmpty<T = unknown>(val: unknown): val is null | undefined {
    return isNullOrUnDef(val) || isEmpty(val)
}

export function isNumber(val: unknown): val is number {
    return is(val, 'Number')
}

export function isPromise<T = any>(val: unknown): val is Promise<T> {
    return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch)
}

export function isString(val: unknown): val is string {
    return is(val, 'String')
}

export function isFunction(val: unknown): val is Function {
    return is(val, 'Function')
}

export function isBoolean(val: unknown): val is boolean {
    return is(val, 'Boolean')
}

export function isRegExp(val: unknown): val is RegExp {
    return is(val, 'RegExp')
}

export function isArray<T = any>(val: any): val is Array<T> {
    return is(val, 'Array')
}

export function isWindow(val: any): val is Window {
    return typeof window !== 'undefined' && is(val, 'Window')
}

export function isElement(val: unknown): val is Element {
    return isObject(val) && !!val.tagName
}

export function isMap(val: unknown): val is Map<any, any> {
    return is(val, 'Map')
}

export const isServer = typeof window === 'undefined'

export const isClient = !isServer

/* 空数组对象判断*/
export function isEmptyArr(arr: any) {
    return isArray(arr) && arr.length === 0
}
/**
 * 空对象判断
 */
export function isEmptyObject(obj: Record<string, any>): boolean {
    for (const _name in obj) {
        return false
    }
    return true
}

//判断某个数组中是否包含另一个数组/值
export function isContained<T = any>(target: T[], value: T[] | T) {
    if (!(target instanceof Array)) {
      return false
    }
    if (!(value instanceof Array)) {
      return target.includes(value)
    }
    if (target.length < value.length || value.length === 0 || target.length === 0) {
      return false
    }
    for (let i = 0; i < value.length; i++) {
      if (!target.includes(value[i])) {
        return false
      }
    }
    return true
  }
